package net.wofly.sale.shoppingcart.service.impl;

import net.wofly.sale.shoppingcart.entity.CartList;
import net.wofly.sale.shoppingcart.repository.CartListRepository;
import net.wofly.sale.shoppingcart.service.ICartListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;


/**
 * @author siqingwei
 */
@Service("iCartListService")
@Transactional
public class CartListServiceImpl implements ICartListService {
    
    @Autowired
    CartListRepository cartListRepository;
    
    @Override
    public CartList save(CartList entity) {
        return cartListRepository.save(entity);
    }
    
    @Override
    public void delete(CartList entity) {
        cartListRepository.delete(entity);
    }
    
    @Override
    public void delete(String key) {
        cartListRepository.delete(key);
    }
    
    @Override
    public CartList findOne(String key) {
        return cartListRepository.findOne(key);
    }
    
    @Override
    public List<CartList> save(List<CartList> entityList) {
        return (List<CartList>) cartListRepository.save(entityList);
    }
    
    @Override
    public CartList findByShoppingCartIDAndCommodityID(String userID, String commodityID) {
        List<CartList> cartLists = cartListRepository.findByShoppingCartIDAndCommodity_CommodityID(userID, commodityID);
        
        if (cartLists.size() > 0) {
            return cartLists.get(0);
        }
        return null;
    }
    
    @Override
    public CartList findByShoppingCartIDAndCommodityID(String userID, Integer shoppingCartType, String commodityID) {
        List<CartList> cartLists = cartListRepository.findByShoppingCartIDAndShoppingCartTypeAndCommodity_CommodityID(
                userID, shoppingCartType, commodityID);
        if (cartLists.size() > 0) {
            return cartLists.get(0);
        }
        return null;
    }
    
    @Override
    public List<CartList> findByShoppingCartID(String userID) {
        return cartListRepository.findByShoppingCartIDOrderByModifyDate(userID);
    }
    
    @Override
    public List<CartList> findByShoppingCartID(String userID, Integer shoppingCartType) {
        return cartListRepository.findByShoppingCartIDAndShoppingCartTypeOrderByModifyDate(userID, shoppingCartType);
    }
    
    @Override
    public void deleteByShoppingCartID(String shoppingCartID) {
        cartListRepository.deleteByShoppingCartID(shoppingCartID);
    }
    
    @Override
    public void deleteByShoppingCartIDAndShoppingCartType(String cartListID, Integer shoppingCartType) {
        cartListRepository.deleteByShoppingCartIDAndShoppingCartType(cartListID, shoppingCartType);
    }
    
    
}
